$(function() {
	admin.adBase.init();
});
Namespace('admin.adBase', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	toAdd:function() {
		top.addTab("1",'添加', contextPath+'/ad/base/toAdd');
	},
	toUpdate:function(id) {
		if (id != null) {
			top.addTab(id,'编辑', contextPath+'/ad/base/toUpdate?id=' + id);
		}
	},
	toExpireAt: function (id) {
		var tip = "确定失效吗";
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/ad/base/toExpireAt';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					top.addTab("",'广告列表', contextPath+'/ad/base/list');
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	add: function () {
		 	if(!$('#data-form').form('validate')) return;
	        var data=$('#data-form').form('jsonObject');
            var url = contextPath + '/ad/base/create';
			$.post(url, data, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('创建成功');
					top.addTab("",'广告列表', contextPath+'/ad/base/list');
				} else {
					top.showInfo(result.msg);
				}
			});

	},
	addImg:function(id){
		getDialog('query-dialog').dialog({
			href:contextPath+'/ad/base/imgUpload',
			title: '选择文件',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'上传',
				handler:function(){
					uploaderStart(id);
				}
			}]
		});
		
	},
	netMsgChange:function(id,maxCount) {
		var inputLength = $('#'+id).val().length;
		if (inputLength == 0) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + maxCount + '个字');
			saveFlag = false;
		} else if (inputLength > 0 && inputLength <= maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + (maxCount - inputLength) + '个字');
			saveFlag = false;
		} else if (inputLength > maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，已经输入超过<strong class="cred">' + (inputLength - maxCount) + '</strong>个字');
			saveFlag = true;
		}
	},
});